package com.uxwine.alarm;

import com.uxwine.alarm.mon.AlarmNotify;
import com.uxwine.alarm.mon.AlarmUtil;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public abstract class AlarmActivity extends Activity
{
	
    protected static final String SCREEN_OFF = "screen_off";
    
	protected Alarm mAlarm;
	protected int mnHWBehavior;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		mAlarm = getIntent().getParcelableExtra(AlarmUtil.EXTRA_ALARM_INTENT);
        //sign changed by reason
        mAlarm = AlarmUtil.getAlarm(getContentResolver(), mAlarm.id);
		
        mnHWBehavior = Settings.getInstance(this).getHWBehavior(Settings.HWBEHAVIOR_SNOOZE);
        
		requestFullScreen();
		
		onUpdateLayout(mAlarm);
		
		registerReceiver();
	}
	
	/**
     * this is called when a second alarm is triggered while a
     * previous alert window is still active.
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        Log.v("zhangzhongming", "AlarmAlert.OnNewIntent()");

        mAlarm = intent.getParcelableExtra(AlarmUtil.EXTRA_ALARM_INTENT);

        //setTitle();
    }
    
//    @Override
//    protected void onResume() {
//        super.onResume();
//        // If the alarm was deleted at some point, disable snooze.
//        if (AlarmUtil.getAlarm(getContentResolver(), mAlarm.id) == null)
//        {
//        	onSnoozeEnabled(false);
//        }
//    }
    
    protected abstract void onSnoozeEnabled(boolean isEnabled);
    protected abstract boolean isSnoozeEnable();	
	protected abstract void onUpdateLayout(Alarm alarm);
	protected abstract void onSnoozed(Alarm alarm, long time);

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v("zhangzhongming", "AlarmActivity.onDestroy()");
        // No longer care about the alarm being killed.
        unregisterReceiver(mReceiver);
    }

//    @Override
//    public boolean dispatchKeyEvent(KeyEvent event) {
//        // Do this on key down to handle a few of the system keys.
//        boolean up = event.getAction() == KeyEvent.ACTION_UP;
//        switch (event.getKeyCode()) {
//            // Volume keys and camera keys dismiss the alarm
//            case KeyEvent.KEYCODE_VOLUME_UP:
//            case KeyEvent.KEYCODE_VOLUME_DOWN:
//            case KeyEvent.KEYCODE_CAMERA:
//            case KeyEvent.KEYCODE_FOCUS:
//                if (up) {
//                    switch (mnHWBehavior) {
//                        case 1:
//                            snooze();
//                            break;
//
//                        case 2:
//                            dismiss(false);
//                            break;
//
//                        default:
//                            break;
//                    }
//                }
//                return true;
//            default:
//                break;
//        }
//        return super.dispatchKeyEvent(event);
//    }
    
    @Override
    public void onBackPressed() {
        // Don't allow back to dismiss. This method is overriden by AlarmAlert
        // so that the dialog is dismissed.
        return;
    }

	protected void requestFullScreen()
	{
		requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);

		final Window win = getWindow();
		win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
				| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
		// Turn on the screen unless we are being launched from the AlarmAlert
		// subclass.
		if (!getIntent().getBooleanExtra(SCREEN_OFF, false))
		{
			win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
					| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
					| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
		}
	}
	
	public void snooze()
	{
		if(!isSnoozeEnable())
		{
			dismiss(false);
			return;
		}
		
		final Settings s = Settings.getInstance(this);
		final long d = s.getSnoozeDuration(Settings.SNOOZE_DURATION_DEFAULT);
		final long snoozeTime = System.currentTimeMillis() + d * 1000L * 60L;
		AlarmUtil.saveSnoozeAlert(this, mAlarm.id, snoozeTime);
		
		//new AlarmNotify(this).notifySnoozed(mAlarm);
		
		new AlarmNotify(this).updateStated(mAlarm, 2);
		
		onSnoozed(mAlarm, snoozeTime);
		
		stopService(new Intent(AlarmUtil.ACTION_ALARM_ALERT));
		
		//finish();
	}

	public void dismiss(boolean killed)
	{
		if (!killed)
		{
			//new AlarmNotify(this).cancel(mAlarm.id);
			stopService(new Intent(AlarmUtil.ACTION_ALARM_ALERT));
		}
		finish();
	}
	
	// Register to get the alarm killed/snooze/dismiss intent.
	private void registerReceiver()
	{		
        IntentFilter filter = new IntentFilter(AlarmUtil.ACTION_ALARM_KILLED);
        filter.addAction(AlarmUtil.ACTION_ALARM_DISMISS);
        filter.addAction(AlarmUtil.ACTION_ALARM_SNOOZE);
        registerReceiver(mReceiver, filter);
	}

	// Receives the ALARM_KILLED action from the AlarmKlaxon,
	// and also ALARM_SNOOZE_ACTION / ALARM_DISMISS_ACTION from other
	// applications
	private BroadcastReceiver mReceiver = new BroadcastReceiver()
	{
		@Override
		public void onReceive(Context context, Intent intent)
		{
			String action = intent.getAction();
			if (action.equals(AlarmUtil.ACTION_ALARM_SNOOZE))
			{
				snooze();
			} 
			else if (action.equals(AlarmUtil.ACTION_ALARM_DISMISS))
			{
				dismiss(false);
			} 
			// if(AlarmUtil.ACTION_ALARM_KILLED
			else
			{
				Alarm alarm = intent.getParcelableExtra(AlarmUtil.EXTRA_ALARM_INTENT);
				if (alarm != null && mAlarm.id == alarm.id)
				{
					dismiss(true);
				}
			}
		}
	};
}
